﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.Infra.Service.Common;
using System.Data;

namespace Sinacor.OMM.Deployment.Configurator.ResourceAccess
{
    public class Router : BaseResourceAccess
    {

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public Router(Int32 companyId)
            : base(companyId)
        {

        }

        #endregion

        #region Methods

        public Entity.Router ConsultarRoteador()
        {
            //
            String sqlCommand = String.Empty;
            Entity.Router retorno = null;

            sqlCommand = @"SELECT a.TEM_REPE_TMSS
                                  ,a.COD_REPRIO_MSG_ROTM
                                  ,a.COD_REPRIO_MSG_COMU_ROTR
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'CONNECTION_STRING' AND COD_REPRIO = a.COD_REPRIO_MSG_ROTM) DESC_REPRIO_MSG_ROTM
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_MESSAGE_LIMIT' AND COD_REPRIO = a.COD_REPRIO_MSG_ROTM) MSG_LIMIT_REPRIO_MSG_ROTM
                                  ,(SELECT TXT_CNTD_PRDD FROM TBOREPRIO_DET WHERE NOME_PRDD = 'QUEUE_BYTES_LIMIT' AND COD_REPRIO = a.COD_REPRIO_MSG_ROTM) BYTES_LIMIT_REPRIO_MSG_ROTM
                           FROM TBOCNFG_ROTR a";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                retorno = this.FindOne<Entity.Router>(command,
                    reader =>
                    {
                        Entity.Router item = new Entity.Router();

                        item.TimeOutRetransmissao = reader.GetValue<int?>("TEM_REPE_TMSS");
                        item.Repositorio.Codigo = reader.GetValue<int>("COD_REPRIO_MSG_ROTM");
                        item.Repositorio.Fila = reader.GetValue<String>("DESC_REPRIO_MSG_ROTM");
                        item.Repositorio.LimiteMensagem = reader.GetValue<int>("MSG_LIMIT_REPRIO_MSG_ROTM");
                        item.Repositorio.LimiteTamanho = reader.GetValue<int>("BYTES_LIMIT_REPRIO_MSG_ROTM");

                        return item;
                    });
            }

            return retorno;
        }

        public void AlterarRoteador(Entity.Router entity)
        {
            String sqlCommand = String.Empty;

            sqlCommand = @"UPDATE TBOCNFG_ROTR SET
                                   TEM_REPE_TMSS = :TEM_REPE_TMSS";

            using (DbCommand command = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(command, "TEM_REPE_TMSS", DbType.Int32, entity.TimeOutRetransmissao);

                this.DataBase.ExecuteNonQuery(command);
            }
        }

        #endregion
    }
}